package org.tis.tools.abf.module.sys.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.sys.controller.request.SysDictItemRequest;
import org.tis.tools.abf.module.sys.controller.request.SysDictItemValueRequest;
import org.tis.tools.abf.module.sys.controller.request.SysRoleGroupRequest;
import org.tis.tools.abf.module.sys.entity.SysDict;
import org.tis.tools.abf.module.sys.entity.SysDictItem;
import org.tis.tools.abf.module.sys.service.ISysDictItemService;
import org.tis.tools.abf.module.sys.service.ISysDictService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * sysDictItem的Controller类
 *
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/sysDictItems")
@Validated
@Api(description = "业务字典项")
@Slf4j
public class SysDictItemController  extends BaseController {
    @Autowired
    private ISysDictItemService iSysDictItemService;
    @Autowired
    private ISysDictService iSysDictService;

    @OperateLog(type = OperateType.ADD, desc = "新增业务字典项")
    @ApiOperation(value = "新增业务字典项", notes = "传入SysDictItemRequest类作为参数")
    @PostMapping
    public ResultVO addSysDictItem(@RequestBody @Validated @ApiParam(value = "SysDictItemRequest",name = "传入的SysDictItemRequest类", required = true) SysDictItemRequest request) {
        if (iSysDictItemService.existItemValue(request.getGuidDict(),request.getItemValue())){
            return ResultVO.error("404", "该字典项已经存在!");
        }
        if (iSysDictItemService.existItemName(request.getGuidDict(),request.getItemName())){
            return ResultVO.error("404","字典项名称已经存在!");
        }

        SysDictItem sysDictItem1 = iSysDictItemService.addDictItem(request);
        return ResultVO.success("添加成功",sysDictItem1);
    }


    @OperateLog(type = OperateType.UPDATE, desc = "修改业务字典项")
    @ApiOperation(value = "修改业务字典项", notes = "传入SysDictItemRequest类作为参数")
    @PutMapping
    public ResultVO updateSysDictItem(@RequestBody @Validated @ApiParam(value = "SysDictItemRequest",name = "传入的SysDictItemRequest类", required = true) SysDictItemRequest request) {
        SysDictItem sysDictItemQue = iSysDictItemService.guidQueryOneSysDic(request.getGuid());
        if (null == sysDictItemQue){
            return ResultVO.error("404", "找不到对应的字典项记录或已经被删除！");
        }
        SysDictItem sysDictItem = iSysDictItemService.editSysDictItem(request);
        return ResultVO.success("修改成功",sysDictItem);
    }

    @OperateLog(type = OperateType.DELETE, desc = "删除业务字典项")
    @ApiOperation(value = "删除业务字典项", notes = "根据guid删除对应的数据信息")
    @DeleteMapping("/{id}")
    public ResultVO editSysDictItem(@PathVariable @NotBlank(message = "ID不能为空") @ApiParam(value = "id",name = "业务字典项GUID", required = true) String id) {
        SysDictItem sysDictItem = iSysDictItemService.guidQueryOneSysDic(id);
        if (null == sysDictItem){
            return ResultVO.error("404", "找不到对应的字典项记录或已经被删除！");
        }
        sysDictItem = iSysDictItemService.deleteDictItem(id);
        return ResultVO.success("删除成功",sysDictItem);
    }


    @ApiOperation(value = "查询业务字典项", notes = "根据guidDict查询对应的业务字典信息")
    @PostMapping("/listDict/{id}")
    public ResultVO querySysDictItem(@PathVariable @NotBlank(message = "ID不能为空") @ApiParam(value = "id",name = "业务字典项GUID", required = true) String id){
        SysDictItem sysDictItem = iSysDictItemService.guidQueryOneSysDic(id);
        SysDict sysDict = new SysDict();
        if (null != sysDictItem){
            sysDict = iSysDictService.queryOneSysDictByGuid(sysDictItem.getGuidDict());
        }else {
            return ResultVO.error("404", "找不到对应的字典项记录或已经被删除！");
        }
        return ResultVO.success("查询成功",sysDict);
    }


    @ApiOperation(value = "查询单条业务字典项", notes = "根据guid查询出对应的数据信息")
    @GetMapping("/{id}")
    public ResultVO querySysDictItemList(@PathVariable @NotBlank(message = "ID不能为空") @ApiParam(value = "id",name = "业务字典项GUID", required = true) String id) {
        SysDictItem sysDictItem = iSysDictItemService.guidQueryOneSysDic(id);
        if (null == sysDictItem){
            return ResultVO.error("404", "找不到对应的字典项记录或已经被删除！");
        }
        return ResultVO.success("查询成功",sysDictItem);
    }


    @ApiOperation(value = "查询所有业务字典项", notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO queryAllDictItem(@RequestBody @Validated @ApiParam(value = "page",name = "分页信息", required = true) SmartPage<SysDictItem> page) {
        return ResultVO.success("查询成功", iSysDictItemService.querySysDictItemList(page.getPageObj(), page.getConditionWrapper()));
}


    @ApiOperation(value = "查询业务字典下的所有字典项", notes = "根据业务字典guid查询出所有字典项的信息")
    @GetMapping("/queryListByDictId/{id}")
    public ResultVO queryByDictId(@PathVariable @NotBlank(message = "业务字典ID不能为空") @ApiParam(value = "id",name = "业务字典GUID", required = true) String id) {
        return ResultVO.success("查询成功",iSysDictItemService.querySysDictItems(id));
    }

    @ApiOperation(value = "根据key查询业务字典下的所有字典项", notes = "根据业务字典key查询出所有字典项的信息")
    @GetMapping("/queryListByDictKey/{dictKey}")
    public ResultVO queryListByDictKey(@PathVariable @NotBlank(message = "业务字典key不能为空") @ApiParam(value = "dictKey",name = "业务字典key", required = true) String dictKey) {
        return ResultVO.success("查询成功",iSysDictItemService.querySysDictItemsByKey(dictKey));
    }


    @ApiOperation(value = "判断字典项字段是否已经存在", notes = "传入SysDictItemValueRequest类作为参数")
    @PostMapping("/existItemValue")
    public ResultVO existItemValue(@RequestBody @Validated @ApiParam(value = "SysDictItemValueRequest",name = "传入的SysDictItemValueRequest类", required = true) SysDictItemValueRequest itemValueRequest) {
        return ResultVO.success("查询成功",iSysDictItemService.existItemValue(itemValueRequest.getGuidDict(),
                itemValueRequest.getItemValue()));
    }


    @ApiOperation(value = "判断字典项名称是否已经存在", notes = "需要传入字典项名称(itemName)和隶属业务字典(guidDict)字段为参数")
    @PostMapping("/{guidDict}/existItemValue/{itemName}")
    public ResultVO existItemName(@PathVariable @NotBlank(message = "隶属业务字典不能为空") @ApiParam(value = "guidDict",name = "隶属业务字典",required = true) String guidDict, @PathVariable @NotBlank(message = "字典项名称不能为空") @ApiParam(value = "itemName",name = "字典项名称",required = true) String itemName) {
        return ResultVO.success("查询成功",iSysDictItemService.existItemName(guidDict,itemName));
    }

    /** 下面为 角色分组接口 */

    @OperateLog(type = OperateType.ADD, desc = "新增角色分组")
    @ApiOperation(value = "新增角色分组", notes = "传入SysRoleGroupRequest类作为参数")
    @PostMapping("/addRoleGroup")
    public ResultVO addRoleGroup(@RequestBody @Validated @ApiParam(value = "SysRoleGroupRequest",name = "传入的SysRoleGroupRequest类", required = true) SysRoleGroupRequest roleGroupRequest) {
        if (iSysDictItemService.existItemName(iSysDictService.queryDictByKey("DICT_ROLE_GROUP").getGuid(),roleGroupRequest.getItemName
                ())){
            return ResultVO.error("404","角色组名称已经存在!");
        }
        SysDictItem sysDictItem1 = iSysDictItemService.addRoleGroup(roleGroupRequest);
        return ResultVO.success("添加成功",sysDictItem1);
    }

    @OperateLog(type = OperateType.UPDATE, desc = "修改角色组")
    @ApiOperation(value = "修改角色组", notes = "传入SysDictItemRequest类作为参数")
    @PutMapping("/updateRoleGroup")
    public ResultVO updateRoleGroup(@RequestBody @Validated @ApiParam(value = "SysDictItemRequest",name = "传入的SysDictItemRequest类", required = true) SysDictItemRequest request) {
        SysDictItem sysDictItemQue = iSysDictItemService.guidQueryOneSysDic(request.getGuid());
        if (null == sysDictItemQue){
            return ResultVO.error("404", "找不到对应的字典项记录或已经被删除！");
        }
        SysDictItem sysDictItem = iSysDictItemService.editSysDictItem(request);
        return ResultVO.success("修改成功",sysDictItem);
    }

    @OperateLog(type = OperateType.DELETE, desc = "删除角色组")
    @ApiOperation(value = "删除角色组", notes = "根据guid删除对应的数据信息")
    @DeleteMapping("/{id}/deleteRoleGroup")
    public ResultVO deleteRoleGroup(@PathVariable @NotBlank(message = "ID不能为空") @ApiParam(value = "id",name = "业务字典项GUID", required = true) String id) {
        SysDictItem sysDictItem = iSysDictItemService.guidQueryOneSysDic(id);
        if (null == sysDictItem){
            return ResultVO.error("404", "找不到对应的字典项记录或已经被删除！");
        }
        sysDictItem = iSysDictItemService.deleteDictItem(id);
        return ResultVO.success("删除成功",sysDictItem);
    }


}
